import Vue from 'vue'
import VueRouter from 'vue-router'
//使用插件
Vue.use(VueRouter)
    //引入路由组件
import Home from '@/pages/Home'
import Search from '@/pages/Search'
import Login from '@/pages/Login'
import Register from '@/pages/Register'
//先把VueRouter原型对象的push，保存一份
let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace
    //重写push replcae
VueRouter.prototype.push = function(location, resolve, reject) {
    if (resolve && reject) {
        originPush.call(this, location, resolve, reject)
    } else {
        originPush.call(this, location, () => {}, () => {})
    }
}
VueRouter.prototype.replace = function(location, resolve, reject) {
        if (resolve && reject) {
            originReplace.call(this, location, resolve, reject)
        } else {
            originReplace.call(this, location, () => {}, () => {})
        }
    }
    //配置路由
export default new VueRouter({
    scrollBehavior (to, from, savedPosition) {
          return savedPosition
       
      },
    routes: [{
            path: '/home',
            component: Home,
            meta: {
                show: true
            }
        },
        {
            name: 'search',
            path: '/search/:keyword?', //?表示可传可不传
            component: Search,
            //路由组件传递props数据
            //函数写法
            props: ($route) => {
                return {
                    keyword: $route.params.keyword,
                }
            },
            meta: {
                show: true
            }
        },
        {
            path: '/login',
            component: Login,
            meta: {
                show: false
            }
        },
        {
            path: '/register',
            component: Register,
            meta: {
                show: false
            }
        },
        //重定向，在项目跑起来的时候，访问/，礼吗定向到首页
        {
            path: '*',
            redirect: '/home'
        }
    ]
})